<?php

class Controller_Admin_Chapter extends Controller_Admin_Base
{
	// 列表
    public function action_index()
    {
    	$count = DB::select(DB::expr("COUNT(*) AS total"))
                ->from('chapter')
                ->execute()
                ->get('total');
        $pagination = Pagination::factory(array(
            'total_items' => $count
        ));

        $records = DB::select()->from('chapter')
                ->order_by('order', 'ASC')
                ->order_by('id', 'DESC')
                ->limit($pagination->items_per_page)
                ->offset($pagination->offset)
                ->execute()->as_array();

        $this->prepare('章节列表');
        $this->content->records = $records;
        $this->content->pagination = $pagination->render();
        $this->render();
    }
    
    public function action_add()
    {
    	if ($_POST) {
    		$chapter = ORM::factory('chapter');
    		$chapter->values($_POST);
    		
    		$validate = $chapter->validate();
    		if (!$chapter->check()) {
    			$this->message($validate->errors('chapter'));
    			return;
    		}
    		
    		if (!$chapter->save()) {
                $this->message('操作失败');
                return;
            }
            $this->message('添加成功', 'chapter');
    	} else {
    		$this->prepare('新增章节');
        	$this->render();
    	}
    }
    
    public function action_edit()
    {
    	if ($_POST) {
    		$id = Arr::get($_POST, 'id');
    		
    		unset($_POST['id']);
    		
    		$chapter = ORM::factory('chapter');
    		$chapter->find($id);
            if ( ! $chapter->loaded()) {
                $this->message('指定的记录不存在！');
                return;
            }
    		
    		$chapter->values($_POST);
    		
    		$validate = $chapter->validate();
    		if (!$chapter->check()) {
    			$this->message($validate->errors('chapter'));
    			return;
    		}
    		
    		if (!$chapter->save()) {
                $this->message('操作失败');
                return;
            }
            $this->message('修改成功', 'chapter');
    	} else {
    		$id = (int)$this->request->param('id');

            if (empty($id)) {
                $this->message('参数错误！');
                return;
            }

            $chapter = ORM::factory('chapter')->find($id);

            if ( ! $chapter->loaded()) {
                $this->message('指定的记录不存在！');
                return;
            }
    		$this->prepare('编辑章节');
    		$this->content->record = $chapter->as_array();
        	$this->render();
    	}
    }
    
	// 删除
    public function action_delete() {
        $id = (int) $this->request->param('id');

        if (empty($id)) {
            $this->message('参数错误！');
            return;
        }

        $admin = ORM::factory('chapter')->find($id);

        if ( ! $admin->loaded()) {
            $this->message('指定的记录不存在！');
            return;
        }

        if ($admin->delete()) {
            $this->message('删除成功！', 'chapter');
        } else {
            $this->message('删除失败');
        }
    }
}